<template>
	<el-container>
		<el-header style="height: auto;">
		<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="levelId" @selection-change="selectionChange" stripe>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="标识" prop="levelId" width="300"></el-table-column>
				<el-table-column label="组织名称" prop="orgName" width="200"></el-table-column>
				<el-table-column label="职务名称" prop="levelName" width="150"></el-table-column>
				<el-table-column label="直属上级" prop="parentLevelName" width="150"></el-table-column>
				<el-table-column label="备注" prop="remark" width="400"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="120">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
import saveDialog from './save'
import scSelectFilter from '@/components/scSelectFilter'
export default {
	name: 'userLevel',
	components: {
		saveDialog,
		scSelectFilter
	},
	data() {
		return {
			dialog: {
				save: false
			},
			props: {
				label: 'label',					//映射label显示字段
				value: 'value',					//映射value值字段
			},
			filterData: [
				{
					title: "虚拟组织",
					key: "tempOrgId",
					multiple: true,
					options:[{
						label: "全部",
						value: ""
					}]
				}
			],
			apiObj: this.$API.system.tempOrg.getTempUserLevelListForManage,
			selection: []
		}
	},
	mounted() {
		this.getPendingTempUnitList();
	},
	methods: {
		async getPendingTempUnitList()
		{
			var res = await this.$API.system.tempOrg.getPendingTempUnitList.get();
			if (res.code == 200) {
				this.filterData[0].options = res.data
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//增加
		add(){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		filterChange(data){
			this.$refs.table.upData(data)
		},
		//编辑
		table_edit(row){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//删除
		async table_del(row, index){
			var res = await this.$API.system.tempOrg.deleteTempUserLevel.post({levelId: row.levelId});
			if(res.code == 200){
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			}else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del(){
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				const loading = this.$loading();
				var idsArr=[];
				this.selection.forEach(item => {
					idsArr.push(item.levelId)
				})
				var res = await this.$API.system.tempOrg.deleteTempUserLevelByIds.post({ids: idsArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
				this.$message.success("操作成功")
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		//本地更新数据
		handleSuccess(){
			this.$refs.table.refresh()
		}
	}
}
</script>

<style>
</style>
